package com.mes.web.controller.cb;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.mes.entity.cb.*;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mes.service.cb.OderDetailedService;
import com.mes.service.cb.OrderService;
import com.mes.service.tdd.OthersService;
import com.mes.utils.CustomUtils;
import com.mes.utils.DateJsonValueProcessor;
import com.mes.utils.ResponseUtil;
import com.mes.utils.StringUtil;
import com.mes.utils.formatUtils;

/**
 * @ClassName: OderDetailed 
 * @Description:订单详细
 * @Author cb
 * @Date 2017年12月26日 下午3:12:04 
 * @version 1.0
 */
@Controller
@RequestMapping("/oderDetailed")
public class OderDetailedController {

	@Resource
	private OderDetailedService oderDetailedService;
	
	@Resource
	private OrderService orderService;
	@Resource
	private OthersService othersService;
	
	
	@RequestMapping("/oderDetailed_list")
	public void oderDetailed_list(HttpServletResponse response,String limit,String offset,String number) throws Exception {





			PageBean pages = CustomUtils.paging(limit, offset);
			pages.setNumber(number);
			List<OrderDetailed> detaileds=oderDetailedService.oderDetailed_list(pages);

			int getTotal=oderDetailedService.oderDetailed_total(pages);


			JSONObject jsonObject = new JSONObject();
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
			JSONArray jsonArray = JSONArray.fromObject(detaileds, jsonConfig);
			jsonObject.put("total", getTotal);
			jsonObject.put("rows", jsonArray);
			ResponseUtil.write(response, jsonObject);





	}
	
	/**
	 * 分页
	 * @param response
	 * @param limit
	 * @param offset
	 * @throws Exception 
	 */
	@RequestMapping("/list")
	public void list(HttpServletResponse response,String limit,String offset,String plannumber) throws Exception {
		
		PageBean page = CustomUtils.paging(limit, offset);
		page.setPlanNumber(plannumber);
		List<PlanDetailed> planDetaileds= oderDetailedService.list(page);
		int total=oderDetailedService.getTotal(page);
		
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(planDetaileds, jsonConfig);
		jsonObject.put("total", total);
		jsonObject.put("rows", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 添加数据到订单中
	 * @param order_number 订单编号
	 * @param order_time 订单时间
	 * @param supper_id 供应商
	 * @param arrTime 到达时间
	 * @param materiel_id 物料ID
	 * @param count 订单数量
	 * @param plandId 计划详情id
	 * @throws Exception 
	 */
	@RequestMapping("/orderDetailedAdd")
	public void orderDetailedAdd(String prodId,HttpServletResponse response , String order_number,String order_time,String supper_id,String arrTime,String materiel_id,String count,String plandId) throws Exception {
		JSONObject jsonObject=new JSONObject();
		boolean b =  orderService.selectOrderBynumber(order_number);
		
		if (b) {
			jsonObject.put("success", 3);
			ResponseUtil.write(response, jsonObject);
			return ;
		}
		System.out.println(prodId+"------------------------------------------------------------------------");
		
		Order order=new Order();
		//订单编号
		order.setNumber(order_number);
		//订单时间
		order.setOrdertime(formatUtils.formateDate(order_time));
		//供应商id
		order.setRemark1(supper_id);
		//审批状态
		order.setState("0");
		//计划号
		order.setRemark3(prodId);


		String[] split1 = materiel_id.split(",");
		String[] split2 = count.split(",");
		String[] split3 = arrTime.split(",");
		String[] split4 = plandId.split(",");
		int index=0;
		int total=0;
		for (int i = 0; i < split1.length; i++) {
		
			OrderDetailed orderDetailed=new OrderDetailed();
			//物料id
			orderDetailed.setMaterieid(Long.parseLong(split1[i]));
			//数量
			orderDetailed.setCount(split2[i]);
			//采购订单编号
			orderDetailed.setNumber(order_number);
			//到达时间
			orderDetailed.setRemark1(split3[i]);
			orderDetailed.setRemark2(split4[i]);
			int orderDe_total=oderDetailedService.orderDe_insert(orderDetailed);
			total+=orderDe_total;
			index++;
		}
		int order_total=orderService.order_insert(order);
		
		
		
		if (order_total>0 && index==total) {
			othersService.caiPlanDetailedUpRemark14(plandId, "1");
			jsonObject.put("success", 1);
		}else {
			jsonObject.put("success", 2);
		}
		
		ResponseUtil.write(response, jsonObject);
	}

	/**
	 * 添加数据到订单中(修改先删除在添加一遍)
	 * @param order_number 订单编号
	 * @param order_time 订单时间
	 * @param supper_id 供应商
	 * @param arrTime 到达时间
	 * @param materiel_id 物料ID
	 * @param count 订单数量
	 * @param plandId 计划详情id
	 * @throws Exception
	 */
	@RequestMapping("/orderDetailedAdd_update")
	public void orderDetailedAdd_update(String prodId ,HttpServletResponse response , String order_number,String order_time,String supper_id,String arrTime,String materiel_id,String count,String plandId) throws Exception {
		
		//根据订单编号删除订单及详情及修改对应采购计划单详情
		int totals = oderDetailedService.orderDetailed_delete(order_number);

		System.out.println(totals);

		JSONObject jsonObject=new JSONObject();
		//判断是否是有值
		boolean b =  orderService.selectOrderBynumber(order_number);
		//重复操作
		if (b) {
			jsonObject.put("success", 3);
			ResponseUtil.write(response, jsonObject);
			return ;
		}

		Order order=new Order();
		//订单编号
		order.setNumber(order_number);
		//订单时间
		order.setOrdertime(formatUtils.formateDate(order_time));
		//供应商id
		order.setRemark1(supper_id);
		//审批状态
		order.setState("0");
		//计划号
		order.setRemark3(prodId);

		String[] split1 = materiel_id.split(",");
		String[] split2 = count.split(",");
		String[] split3 = arrTime.split(",");
		String[] split4 = plandId.split(",");
		int index=0;
		int total=0;

		for (int i = 0; i < split1.length; i++) {

			OrderDetailed orderDetailed=new OrderDetailed();
			//物料id
			orderDetailed.setMaterieid(Long.parseLong(split1[i]));
			//数量
			orderDetailed.setCount(split2[i]);
			//采购订单编号
			orderDetailed.setNumber(order_number);
			//到达时间
			orderDetailed.setRemark1(split3[i]);
			orderDetailed.setRemark2(split4[i]);
			int orderDe_total=oderDetailedService.orderDe_insert(orderDetailed);
			total+=orderDe_total;
			index++;

			orderDetailed = null;
		}

		int order_total=orderService.order_insert(order);

		if (order_total>0 && index==total) {
			othersService.caiPlanDetailedUpRemark14(plandId, "1");
			jsonObject.put("success", 1);
		}else {
			jsonObject.put("success", 2);
		}

		ResponseUtil.write(response, jsonObject);
	}





	/**
	 * 将内容添加到添加表中
	 * @param response
	 * @param plan_id  计划单号
	 * @param arrTime  到达时间
	 * @param counts   数量
	 * @param materieId  物料ID
	 * @throws Exception 
	 */
	@RequestMapping("/orderDetailedAdd2")
	public void orderDetailedAdd2(HttpServletResponse response,String plan_id , String arrTime,String counts, String materieId) throws Exception {
		
		String[] materieIds = materieId.split(",");
		String[] count = counts.split(",");
		String[] arrTimes = arrTime.split(",");
		for (int i = 0; i < materieIds.length; i++) {
			OrderDetailedadd detailedadd=new OrderDetailedadd();
			//物料ID
			detailedadd.setMaterieid(Long.parseLong(materieIds[i]));
			//数量
			detailedadd.setCount(count[i]);
			//到达时间
			detailedadd.setArrtime(arrTimes[i]);
			//计划单号
			detailedadd.setRemark1(plan_id);
			
			oderDetailedService.orderDetailedAdd2(detailedadd);
		}
		
		JSONObject object=new JSONObject();
		object.put("success", true);
		ResponseUtil.write(response, object);
	}
	
	/**
	 * 添加表列表
	 * @param response
	 * @param limit
	 * @param offset
	 * @param plannumber
	 * @throws Exception 
	 */
	@RequestMapping("/list2")
	public void list2(HttpServletResponse response,String limit,String offset,String plannumber) throws Exception {
		PageBean page = CustomUtils.paging(limit, offset);
		if (StringUtil.isEmpty(plannumber)) {
			plannumber="0";
		}
		page.setPlanNumber(plannumber);
		List<OrderDetailedadd> detailedadds= oderDetailedService.list2(page);
		int getDetailedaddsTotal=oderDetailedService.getTotal2(page);
		
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(detailedadds, jsonConfig);
		jsonObject.put("total", getDetailedaddsTotal);
		jsonObject.put("rows", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}
	
	@RequestMapping("/oderDetailedBySumTotal")
	public void oderDetailedBySumTotal(String number,HttpServletResponse respone) throws Exception {
		Long total=oderDetailedService.oderDetailedBySumTotal(number);
		ResponseUtil.write(respone, total);
	}

	@RequestMapping("/oderDetailed_selectByNumber")
	public void oderDetailed_selectByNumber(String number,HttpServletResponse response) throws Exception {
		List<OrderDetailed> list = oderDetailedService.oderDetailed_selectByNumber(number);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		jsonObject.put("total", null);
		jsonObject.put("rows", jsonArray);
		ResponseUtil.write(response, jsonObject);

	}




}
